﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.SharePoint;

namespace Sapphire.Linq.Framework
{
    public class ListQuery<T> : Query<T>, IListQuery
    {
        public ListQuery(IQueryProvider provider, SPList list)
            : base(provider)
        {
            if (list == null)
            {
                throw new ArgumentNullException("list");
            }

            List = list;
        }

        public ListQuery(IQueryProvider provider, Expression expression)
            : base(provider, expression)
        {
            List = ExpressionHelper.GetList(expression);
        }

        public SPList List { get; private set; }

        public LinqQueryProvider LinqQueryProvider
        {
            get
            {
                return Provider as LinqQueryProvider;
            }
        }

        public override IEnumerator<T> GetEnumerator()
        {
            return LinqQueryProvider.Execute<IEnumerable<T>>(Expression, List).GetEnumerator();
        }
    }
}